﻿using IOA.MES.Client.Common;
using IOA.MES.Client.Framing;
using IOA.MES.Client.Models.Response;
using NLog;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Windows;

namespace IOA.MES.Client.Settings
{
    /// <summary>
    /// ImageSettingsIndex.xaml 的交互逻辑
    /// </summary>
    public partial class ImageSettingsIndex : Window
    {
        public ImageSettingsIndex()
        {
            InitializeComponent();
            //FramingIndex = index;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.cbBarCode.IsChecked = FramingIndex.IsPrintCode;
            this.cbDate.IsChecked = FramingIndex.IsPrintDate;
            this.cbLine.IsChecked = FramingIndex.IsPrintLine;
            this.cbTime.IsChecked = FramingIndex.IsPrintTime;
            this.txtPhotoFolder.Text = FramingIndex.PhotoFile;
        }


        /// <summary>
        /// 选择图片保存路径
        /// </summary>
        private void btnSelectPhotoFile_Click(object sender, RoutedEventArgs e)
        {
            var dialog = new System.Windows.Forms.FolderBrowserDialog();
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK && Directory.Exists(dialog.SelectedPath))
            {
                txtPhotoFolder.Text = dialog.SelectedPath;
                FramingIndex.PhotoFile = dialog.SelectedPath;
                BaseEnv.SetStorage("FramePhotoFile", FramingIndex.PhotoFile);
                //var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                //var settings = configFile.AppSettings.Settings;
                //settings["FramePhotoFile"].Value = FramingIndex.PhotoFile;
                //configFile.Save(ConfigurationSaveMode.Modified);
                //ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);
            }
        }
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            FramingIndex.IsPrintCode = this.cbBarCode.IsChecked.Value;
            FramingIndex.IsPrintDate = this.cbDate.IsChecked.Value;
            FramingIndex.IsPrintTime = this.cbTime.IsChecked.Value;
            FramingIndex.IsPrintLine = this.cbLine.IsChecked.Value;
            BaseEnv.SetStorage("IsPrintCode", FramingIndex.IsPrintCode.ToString());
            BaseEnv.SetStorage("IsPrintDate", FramingIndex.IsPrintDate.ToString());
            BaseEnv.SetStorage("IsPrintTime", FramingIndex.IsPrintTime.ToString());
            BaseEnv.SetStorage("IsPrintLine", FramingIndex.IsPrintLine.ToString());
            //var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            //var settings = configFile.AppSettings.Settings;//CbTakePhotos
            //settings["IsPrintCode"].Value = FramingIndex.IsPrintCode.ToString();
            //settings["IsPrintDate"].Value = FramingIndex.IsPrintDate.ToString();
            //settings["IsPrintTime"].Value = FramingIndex.IsPrintTime.ToString();
            //settings["IsPrintLine"].Value = FramingIndex.IsPrintLine.ToString();
            //configFile.Save(ConfigurationSaveMode.Modified);
            //ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);
            this.Close();
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            if (this.DialogResult == true)
            {
                Environment.Exit(0);
            }
        }
    }
}
